﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using DadaLib;

namespace DadaConsole
{
    class Program
    {
        private static string _outputFileName = "result.txt";
        private static int _numberOfSentences = 5;
        private static int _maxCharacterCount = 120;
        private static int _minCharacterCount = 80;
        private static string _inputFile = "odyssey";

        static void Main(string[] args)
        {
            AnalyzeArguments(args);
            Generator gen;
           
            var rawFile = String.Format("{0}.txt", _inputFile);
            var compiledFile = String.Format("{0}.gen", _inputFile);

            if (File.Exists(compiledFile))
                gen = Generator.ReadFromFile(compiledFile);
            else
            {
                gen = new Generator(rawFile);
                gen.Analize();
                gen.SaveToFile(compiledFile);
            }

            using (var fs = File.AppendText(_outputFileName))
            {
                var comp = new Composer(gen);
                var actualNumberOfSentences = 0;
                do
                {
                    var sentence = comp.ComposeSentence();
                    if ((sentence.Length > _maxCharacterCount) || (sentence.Length < _minCharacterCount))
                        continue;
                    fs.WriteLine(sentence);
                    actualNumberOfSentences++;
                } while (actualNumberOfSentences < _numberOfSentences);
                fs.Flush();
            }
        }

        static void AnalyzeArguments(IList<string> args)
        {
            for (var i = 0; i < args.Count; i++)
            {
                if (args[i] == "-i")
                    _inputFile = args[++i];
                if (args[i] == "-o")
                    _outputFileName = args[++i];
                if (args[i] == "-p")
                    _numberOfSentences = Int32.Parse(args[++i]);
                if (args[i] == "-c")
                    _maxCharacterCount = Int32.Parse(args[++i]);
            }
        }
    }
}
